//
// server.cpp
// ~~~~~~~~~~
//
// Copyright (c) 2003-2013 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#include <ctime>
#include <iostream>
#include <string>
#include <boost/array.hpp>
#include <boost/asio.hpp>
#include <boost/thread.hpp>


using boost::asio::ip::tcp;

std::string make_daytime_string()
{
  using namespace std; // For time_t, time and ctime;
  time_t now = time(0);
  return ctime(&now);
}


void workerFunc(std::string message, tcp::socket *socket)
{


	//std::cout << "Worker: running" << std::endl;

	try
	{
		/* if (argc != 2)
		{
		std::cerr << "Usage: client <host>" << std::endl;
		return 1;
		}*/



		//for (;;)
		//{

			boost::system::error_code ignored_error;
			boost::asio::write(*socket, boost::asio::buffer(message), ignored_error);

			boost::array<char, 128> buf;
			boost::system::error_code error;
			size_t len = socket->read_some(boost::asio::buffer(buf), error);

			std::cout.write(buf.data(), len);
			std::cout.write("\n",1);

			//if (error == boost::asio::error::eof)
			//  break; // Connection closed cleanly by peer.
			//else if (error)
			//  throw boost::system::system_error(error); // Some other error.
		//}
	}
	catch (std::exception& e)
	{
		std::cerr << e.what() << std::endl;
	}

	//std::cout << "Worker: finished" << std::endl;
}


std::vector<tcp::socket*> sockets;


void sendToAllSockets()
{
	while(1)
	{
		for(int i=0;i<sockets.size(); i++)
		{

			workerFunc("response from server",sockets[i]);
		}



	}

}



int main3()
{
	try
	{

		
		boost::thread workerThread(sendToAllSockets);

		boost::asio::io_service io_service;

		tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 11180));


		for (int i=0;;i++)
		{
			tcp::socket *socket = new tcp::socket(io_service);
			acceptor.accept(*socket);
			


			


			boost::system::error_code ignored_error;
			boost::asio::write(*socket, boost::asio::buffer("2"), ignored_error);
			//socket->write_some(boost::asio::buffer(Ogre::StringConverter::toString(freeId)), ignored_error);

			boost::array<char, 128> buf;
			boost::system::error_code error;
			size_t len = socket->read_some(boost::asio::buffer(buf), error);

			std::cout.write(buf.data(), len);
			std::cout.write("\n",1);

			sockets.push_back( socket);
		}
	}
	catch (std::exception& e)
	{
		std::cerr << e.what() << std::endl;
	}

	return 0;
}




int main_old()
{
  try
  {
    boost::asio::io_service io_service;

    tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 11180));


	 boost::thread workerThread(sendToAllSockets);

    for (int i=0;;i++)
    {
      sockets.push_back( new tcp::socket(io_service));
      acceptor.accept(*sockets[i]);

      std::string message = make_daytime_string();

	 

	 
    }
  }
  catch (std::exception& e)
  {
    std::cerr << e.what() << std::endl;
  }

  return 0;
}